package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.VisMe;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.MyVisvitHistoryPresenter;
import com.example.peibei.service.presenter.VisvitMeHistoryPresenter;
import com.example.peibei.ui.adapter.SeeMeAdapter;
import com.example.peibei.ui.adapter.VisvitHistoryListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class VisitMeHistoryActivity extends WDActivity {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;
    @BindView(R.id.tv_title)
    TextView tv_title;

    List<VisMe.RecordsDTO> orders;
    private SPUtils spUtils;
    private String token;
    private VisvitMeHistoryPresenter myVisvitHistoryPresenter;
    private SeeMeAdapter seeMeAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_i_send_bill;
    }

    @Override
    protected void initView() {
        tv_title.setText("谁看过我");
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(this));
        seeMeAdapter = new SeeMeAdapter(orders);
        order_list.setAdapter(seeMeAdapter);
        order_list.setEmptyView(empty_view);
        myVisvitHistoryPresenter = new VisvitMeHistoryPresenter(new OrderCall());
        seeMeAdapter.setOnItemClickListener((adapter, view, position) -> {
            VisMe.RecordsDTO order = (VisMe.RecordsDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("mid", order.getMid());
            intent(PersonInfoActivityTwo.class, bundle);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        myVisvitHistoryPresenter.reqeust(true, "Bearer " + token);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }


    class OrderCall implements DataCall<VisMe> {
        @Override
        public void success(VisMe data, Object... args) {
            //添加列表并刷新
            if (myVisvitHistoryPresenter.getPage() == 1) {
                orders.clear();
            }
            seeMeAdapter.setNewInstance(data.getRecords());
            seeMeAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
